package com.rgz.controller.weixin;



import com.rgz.pojo.weixin.AccessToken;
import com.rgz.pojo.weixin.Button;
import com.rgz.pojo.weixin.ClickButton;
import com.rgz.pojo.weixin.CommonButton;
import com.rgz.pojo.weixin.Menu;
import com.rgz.pojo.weixin.ViewButton;
import com.rgz.utils.WeiXinUtil;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jackiechan on 2018/1/31/下午8:11
 * 作用是让我们的运营人员访问可以创建菜单
 */

@RestController
public class CreateMenuController {

    /**
     * 创建菜单
     *
     * @return
     */
    @RequestMapping("/createMenu")
    public  String createMenu(){

       
        //转成 json 字符串,然后请求创建菜单的接口
        //1 获取 access_token
        AccessToken accessToken = WeiXinUtil.getAccessToken(WeiXinUtil.APPID, WeiXinUtil.APPSECRET);
     
        if(accessToken==null){
        	  return "400";
        }else {
        	int menu2 = WeiXinUtil.createMenu(init()
        			, accessToken.getToken());//创建菜单
        	if(menu2==0){
        		 return "200";
        	}else {
        		 return "400";
			}
		}
    }
    //初始化菜单的
    public  Menu init(){

       
    	Menu menu = new Menu();
    	List<Button> buttons=new ArrayList<>();//一级菜单
    	//展示
    	ViewButton viewButton=new ViewButton();
    	viewButton.setName("百度一下");
    	viewButton.setUrl("http://www.baidu.com");
    	viewButton.setType("view");
    	//增加一个一级菜单
    	buttons.add(viewButton);
    	
    	ClickButton clickButton = new ClickButton();
    	ViewButton viewButtonr=new ViewButton();
    	viewButtonr.setName("在线商城");
    	viewButtonr.setUrl("https://mall.sinosafe.com.cn/index/");
    	viewButtonr.setType("view");
    	buttons.add(viewButtonr);
    	
    	CommonButton commonButton=new CommonButton();
    	commonButton.setName("服务");
    	clickButton.setKey("goumai01");
    	List<Button> sub_buttons=new ArrayList<>();//二级菜单
    	
    	
    	ViewButton viewButton3=new ViewButton();
    	viewButton3.setName("自助购保");
    	viewButton3.setUrl("http://weixin.tunnel.qydev.com/whd_Insurance");
    	viewButton3.setType("view");
    	
    	ViewButton viewButton2=new ViewButton();
    	viewButton2.setName("保单微服务");
    	viewButton2.setUrl("http://weixin.tunnel.qydev.com/ccx_home_page");
    	viewButton2.setType("view");
    	sub_buttons.add(viewButton2);
    	sub_buttons.add(viewButton3);
    	commonButton.setSub_button(sub_buttons);
    	buttons.add(commonButton);
    	menu.setButton(buttons);
        return menu;
    }
}
